package cn.net.shuxing.yifu.common.service;


import cn.net.shuxing.yifu.common.bean.ShareholderBonus;
import cn.net.shuxing.yifu.common.bean.ShareholderLevel;
import cn.net.shuxing.yifu.common.mapper.ShareholderLevelMapper;
import cn.net.shuxing.yifu.common.util.CommonExample;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShareholderLevelService extends BaseService<ShareholderLevel, ShareholderLevelMapper> {

    public List<ShareholderLevel> searchByLevel(Integer low, Integer high) {
        Example example = new Example(ShareholderLevel.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andGreaterThan("level", low)
                .andLessThan("level", high + 1);
        return mapper.selectByExample(example);
    }

    public List<ShareholderLevel> searchByLevel(Integer level) {
        Example example = new Example(ShareholderLevel.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("level", level);
        return mapper.selectByExample(example);
    }

    public ShareholderLevel getByShareholderId(Integer shareholderId) {
        CommonExample example = new CommonExample(ShareholderBonus.class);
        example.equals("shareholderId", shareholderId);
        return mapper.selectOneByExample(example);
    }

    public int insert(Integer userId, Integer shareholderLevel) {
        ShareholderLevel shareholder = new ShareholderLevel();
        shareholder.setShareholderId(userId);
        shareholder.setLevel(shareholderLevel);
        shareholder.setCreateTime(LocalDateTime.now());
        shareholder.setUpdateTime(LocalDateTime.now());
        shareholder.setDeleted(0);
        insert(shareholder);
        return shareholder.getId();
    }
}
